package com.example.fund;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.example.fund.util.FundUtil1;
import com.example.fund.R;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class IndexSetDialogAdapter extends BaseAdapter
{
	private LayoutInflater mInflater; 
	private List<Map<String,Object>> list;
	private Context context;
	

	public IndexSetDialogAdapter(Context context,List<Map<String,Object>> list){
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		this.list = (list==null?new LinkedList<Map<String,Object>>():list);
	} 
	
	public void apply() {
		FundUtil1.saveIndexSet(list);
/*		Intent i = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(i);*/
		((android.app.Activity)context).finish(); 
		Intent intent = new Intent(context, MainActivity.class);  
		context.startActivity(intent);
	}

	public void addIndex(Map<String, Object> map) {
		Object numberObj = map.get("number");
		Object nameObj = map.get("name");
		
		if(numberObj!=null && numberObj.toString().trim().length()>0
				&& nameObj!=null && nameObj.toString().trim().length()>0)
		{
			for(int i=0;i<list.size();i++)
			{
				Object numberObj1 = list.get(i).get("number");
				if(numberObj==null || (numberObj!=null && numberObj.toString().equals(numberObj1.toString())))
				{
					list.get(i).put(numberObj.toString(), nameObj.toString());
					return;
				}
			}
            list.add(map);
            notifyDataSetChanged();
		}
	}
	@Override
	public int getCount() { 
		return list==null?0:list.size(); 
	} 

	@Override
	public Object getItem(int arg0) { 
		return list.get(arg0); 
	} 

	@Override
	public long getItemId(int arg0) { 
		return 0; 
	} 

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) 
	{
		Map<String,Object> map = (Map<String,Object>)list.get(position);
		ViewHolder holder = null; 
		if (convertView == null) { 
			holder=new ViewHolder();   
			convertView = mInflater.inflate(R.layout.index_set_dialog_item, null); 
			holder.seq = (TextView)convertView.findViewById(R.id.seq); 
			holder.number = (TextView)convertView.findViewById(R.id.number);
			holder.name = (TextView)convertView.findViewById(R.id.name); 
			holder.defDate = (TextView)convertView.findViewById(R.id.defDate); 
			holder.moveup = (TextView)convertView.findViewById(R.id.moveup); 
			holder.movedown = (TextView)convertView.findViewById(R.id.movedown); 
			holder.del = (TextView)convertView.findViewById(R.id.del); 
			convertView.setTag(holder); 
		}else { 

			holder = (ViewHolder)convertView.getTag(); 
		} 


		holder.seq.setText((position+1)+"");
		holder.number.setText(map.get("number")+""); 
		holder.name.setText(map.get("name")+""); 
/*		int enable = 0;
		if(map.get("enable")!=null)
		{
			enable = Integer.valueOf(map.get("enable").toString());
		}*/
		/*		holder.enable.setBackgroundResource(enable==0?R.drawable.checkbox_no2:R.drawable.checkbox_yes2);
		holder.moveup.setBackgroundResource(R.drawable.arrow_up); 
		holder.movedown.setBackgroundResource(R.drawable.arrow_down); 
		holder.del.setBackgroundResource(R.drawable.guanbi); */
		holder.defDate.setText(map.get("defDate")+"");
		holder.moveup.setText("↑");
		holder.movedown.setText("↓");
		holder.del.setText("×");
		holder.del.setTextColor(Color.RED);

		holder.moveup.setOnClickListener(new View.OnClickListener() { 
			@Override
			public void onClick(View view) {
				if(position>=1)
				{
    				Map<String,Object> temp = list.get(position-1);
    				list.set(position-1, list.get(position));
    				list.set(position, temp);
    				notifyDataSetChanged();
				}
			} 
		});
		holder.movedown.setOnClickListener(new View.OnClickListener() { 
			@Override
			public void onClick(View view) { 
				if(position<=list.size()-2)
				{
    				Map<String,Object> temp = list.get(position+1);
    				list.set(position+1, list.get(position));
    				list.set(position, temp);
    				notifyDataSetChanged();
				}
			} 
		});
		holder.del.setOnClickListener(new View.OnClickListener() { 
			@Override
			public void onClick(View view) {
				Map<String, Object> map = list.get(position);
				if(map==null || map.get("number")==null || map.get("number").toString().trim().length()==0)
				{
					return;
				}
				String number = map.get("number").toString().trim();
				if("0000001".equals(number) || "0000905".equals(number)
						|| "1399006".equals(number) || "1399102".equals(number) || "0000000".equals(number))
				{
					return;
				}
				list.remove(position);
				notifyDataSetChanged();
			} 
		});
		
/*		holder.enable.setOnClickListener(new View.OnClickListener() { 
			@Override
			public void onClick(View view) {
				Map<String, Object> map = list.get(position);
				if(map==null || map.get("number")==null || map.get("number").toString().trim().length()==0)
				{
					return;
				}
				String number = map.get("number").toString().trim();
				if("0000001".equals(number) || "0000905".equals(number)
						|| "1399006".equals(number) || "1399102".equals(number) || "0000000".equals(number))
				{
					return;
				}
				int enable = 1;
				if(map.get("enable")!=null)
				{
					enable = Integer.valueOf(map.get("enable").toString());
					enable = (enable==1?0:1);
				}
				map.put("enable", enable);
				notifyDataSetChanged();
			} 
		});*/


		return convertView; 
	} 

	public class ViewHolder{
		public TextView seq; 
		public TextView name; 
		public TextView number; 
//		public TextView enable; 
		public TextView moveup; 
		public TextView movedown; 
		public TextView del; 
		public TextView defDate; 
	}
}
